#delimit;
clear all;
set more off;


use "KrinerandSchicklerJOPrep2.dta";

label variable dem3 "Democrat";
label variable gop3 "Republican";
label variable age "Age";

gen ind3 = .;
replace ind3 = 0 if gop3 == 1;
replace ind3 = 0 if dem3 == 1;
replace ind3 = 1 if gop3 == 0 & dem3 == 0;

gen party_3point = .;
replace party_3point = 2 if gop3 == 1;
replace party_3point = 0 if dem3 == 1;
replace party_3point = 1 if gop3 == 0 & dem3 == 0;


gen treatment = 0;
replace treatment = 1 if epademinvest == 1;
replace treatment = 2 if epagopinvest == 1;
replace treatment = 3 if epaothinvest == 1;

gen treatment_any = 0;
replace treatment_any = 1 if treatment >= 1;


eststo: reg ind3 epademinvest epagopinvest epaothinvest white male age education, robust;
eststo: reg gop3 epademinvest epagopinvest epaothinvest white male age education, robust;
eststo: reg dem3 epademinvest epagopinvest epaothinvest white male age education, robust;

esttab using "Output/KS_Partisanship.tex", note("")  
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  mlabels("Pr(Independent)" "Pr(Republican)" "Pr(Democrat)") r2 
title("Probability Partisanship \label{KSPartisanship}") replace;
eststo clear; 

*Restricting Sample to Respondents with Non-Missing Approval Response;
reg ind3 epademinvest epagopinvest epaothinvest white male age education if epa != ., robust;
reg gop3 epademinvest epagopinvest epaothinvest white male age education if epa != ., robust;
reg dem3 epademinvest epagopinvest epaothinvest white male age education if epa != ., robust;


*Chi-Squared Statistics;
tabulate party_3point treatment, chi2 col;
tabulate party_3point treatment_any, chi2 col;

*Multinomial Logit Results;
mlogit party_3point epademinvest epagopinvest epaothinvest, robust;
mlogit party_3point epademinvest epagopinvest epaothinvest white male age education, robust;



*Ordered Logit of Presidential Treatment with and Without Post-Treatment Partisanship;
eststo: ologit epa epademinvest epagopinvest epaothinvest dem3 gop3 white male age education, robust;
*Marginal Treatment Effect on 5-Point Approval Outcome;
margins, dydx(epagopinvest) atmeans at(epagopinvest = 1 epademinvest = 0 epaothinvest = 0);

eststo: ologit epa epademinvest epagopinvest epaothinvest white male age education, robust;
*Marginal Treatment Effect on 5-Point Approval Outcome;
margins, dydx(epagopinvest) atmeans at(epagopinvest = 1 epademinvest = 0 epaothinvest = 0);

esttab using "Output/KS_PostTreatment.tex", note("")  
label p star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  eqlabels(none) mlabels("" "") keep(  epademinvest epagopinvest epaothinvest dem3 gop3 white male age education)
title("Presidential Approval \label{KSPostTreatment}") replace ;
eststo clear; 

*Linear Model for 5-Point Approval;
reg epa epademinvest epagopinvest epaothinvest white male age education gop3 dem3 , robust;
reg epa epademinvest epagopinvest epaothinvest white male age education , robust;


gen app_pres = .;
replace app_pres = 1 if epa >= 4 & epa != .;
replace app_pres = 0 if epa <= 3 & epa != .;


reg app_pres epademinvest epagopinvest epaothinvest white male age education gop3 dem3 , robust;
reg app_pres epademinvest epagopinvest epaothinvest white male age education , robust;


egen indep_prop = mean(ind3), by(treatment);
egen gop3_prop = mean(gop3), by(treatment);
egen dem3_prop = mean(dem3), by(treatment);

gen party = "";
replace party = "Independent" if ind3 == 1;
replace party = "Republican" if gop3 == 1;
replace party = "Democrat" if dem3 == 1;

gen treatment_str = "";
replace treatment_str = "Control" if treatment == 0;
replace treatment_str = "Dem. Investigation" if treatment == 1;
replace treatment_str = "Rep. Investigation" if treatment == 2;
replace treatment_str = "Generic Criticsm" if treatment == 3;

 
duplicates drop party treatment, force;

keep party treatment_str indep_prop gop3_prop dem3_prop;
rename treatment_str Treatment;

gen party_perc = .;
replace party_perc = 100*dem3_prop if party == "Democrat";
replace party_perc = 100*gop3_prop if party == "Republican";
replace party_perc = 100*indep_prop if party == "Independent";


drop indep_prop gop3_prop dem3_prop;
gsort -party;

rename party Partisanship;

export delimited using "Output/KS_Export_GGPlot.csv", replace;
